<template>
	<view class="body">
		<image src="../../static/img/pic-1.png" class="img"></image>
		<view class="loginbox" v-if="login">
			<view class="title">
				<text>验证码登录</text>
				<view class="tittoo" @click="toPwd">密码登录</view>
			</view>
			<view class="login">
				<view class="num"><input type="number" placeholder="请输入手机号" v-model="mobile" maxlength="11" /></view>
				<view class="line"></view>
				<view class="code">
					<input type="password" placeholder="请输入验证码" v-model="code" />
					<button @click="getcode" :disabled="disabled">{{ codetip }}</button>
				</view>
			</view>
		</view>
		<view class="loginbox" v-if="!login">
			<view class="title">
				<text>密码登录</text>
				<view class="tittoo" @click="toPwd">验证码登录</view>
			</view>
			<view class="login">
				<view class="num"><input type="text" placeholder="请输入账户" v-model="mobile" /></view>
				<view class="line"></view>
				<view class="pass">
					<input type="password" placeholder="请输入密码" v-model="pass" />
					<text @click="gochangepass">忘记密码</text>
				</view>
			</view>
		</view>
		<button class="loginbtn" @click="codelogin">登录</button>
		<view class="reg" @click="goreg">没有账号，立即注册</view>
	</view>
</template>

<script>
import { sendcode, loginByCode, login } from '../../api/user.js';
export default {
	data() {
		return {
			login: true,
			mobile: '',
			code: '',
			pass: '',
			codetip: '获取验证码',
			disabled: false,
			time: 60,
			btnnum: 0
		};
	},
	methods: {
		toPwd() {
			if (this.login == true) {
				this.login = false;
			} else {
				this.login = true;
			}
		},
		// 发送验证码
		async getcode() {
			if (this.mobile == '') {
				uni.showToast({
					title: '手机号不能为空'
				});
			}else{
				const params = {
				mobile: this.mobile
			};
			let res = await sendcode(params);
			this.gettimer();
			console.log(res);
			}
			
		},
		gettimer() {
			this.time = 60;
			this.timer();
		},
		// 倒计时
		timer() {
			if (this.time > 0) {
				this.disabled = true;
				this.time--;
				this.codetip = this.time + '秒';
				setTimeout(this.timer, 1000);
			} else {
				this.time = 0;
				this.codetip = '发送验证码';
				this.disabled = false;
			}
		},
		gochangepass() {
			uni.navigateTo({
				url: '/pages/login/changePassForget'
			});
		},
		// 验证码登录
		async codelogin() {
			if (this.login == true) {
				if (this.mobile == '') {
					uni.showToast({
						title: '手机号不能为空'
					});
				} else if (this.code == '') {
					uni.showToast({
						title: '验证码不能为空'
					});
				} else {
					let res = await loginByCode({ mobile: this.mobile, code: this.code });
					if (res.errCode == 10000) {
						let token = uni.setStorageSync('token', res.data.token);
						let userinfo = uni.setStorageSync('userinfo', res.data.member);
						let buildings = uni.setStorageSync('buildings', res.data.buildings);
						let buildingState = uni.setStorageSync('buildingState', res.data.buildingState);
						uni.switchTab({
							url: '/pages/index/index'
						});
					}
				} // 密码登录
			} else {
				if (this.mobile == '') {
					uni.showToast({
						title: '账号不能为空'
					});
				} else if (this.pass == '') {
					uni.showToast({
						title: '密码不能为空'
					});
				} else {
					let res = await login({ mobile: this.mobile, pass: this.pass });
					if (res.errCode == 10000) {
						let userid = uni.setStorageSync('userid', res.data.userid);
						let token = uni.setStorageSync('token', res.data.token);
						let userinfo = uni.setStorageSync('userinfo', res.data.member);
						let buildings = uni.setStorageSync('buildings', res.data.buildings);
						let buildingState = uni.setStorageSync('buildingState', res.data.buildingState);
						uni.switchTab({
							url: '/pages/index/index'
						});
					}

					console.log(res);
				}
			}
		},
		// 去注册
		goreg() {
			uni.navigateTo({
				url: '/pages/login/reg'
			});
		}
	}
};
</script>
<style lang="less">
@import '@/static/style/login.less';
</style>
